﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace FrameworkXNA.Scenes
{
    class Menu : SceneBase
    {
        Object2D menuBackground;
        Object2D selection;
        Object2D fadeout;

        bool starting;
        bool fading;

        public override void start()
        {
            menuBackground = new Object2D(Game1.instance.Content.Load<Texture2D>("menuBackground")) { x = Game1.instance.graphics.PreferredBackBufferWidth/2, y = Game1.instance.graphics.PreferredBackBufferHeight/2 };
            selection = new Object2D(Game1.instance.Content.Load<Texture2D>("selection")) { x = Game1.instance.graphics.PreferredBackBufferWidth / 2 - 150, y = Game1.instance.graphics.PreferredBackBufferHeight / 2 +30 };
            fadeout = new Object2D(Game1.instance.Content.Load<Texture2D>("blackpixel")) { alpha = 1 };
            fadeout.setHeight(Game1.instance.graphics.PreferredBackBufferHeight);
            fadeout.setWidth(Game1.instance.graphics.PreferredBackBufferWidth);

            starting = true;

            base.start();
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            menuBackground.Draw(spriteBatch);
            selection.Draw(spriteBatch);
            fadeout.Draw(spriteBatch);
            base.draw(spriteBatch);
        }

        public override void update(GameTime gameTime)
        {
            if (starting)
            {
                fadeout.alpha -= 0.01f;

                if (fadeout.alpha <= 0)
                {
                    fadeout.alpha = 0;
                    starting = false;
                }
            }
            
            if (Controller.keyDown(Keys.Down))
            {
                selection.y = Game1.instance.graphics.PreferredBackBufferHeight / 2 + 120;
            }
            if (Controller.keyDown(Keys.Up))
            {
                selection.y = Game1.instance.graphics.PreferredBackBufferHeight / 2 + 30;
            }

            if (Controller.keyPressed(Keys.Enter))
            {
                if (selection.y == Game1.instance.graphics.PreferredBackBufferHeight / 2 + 30)
                {
                    fading = true;
                }
                else if (selection.y == Game1.instance.graphics.PreferredBackBufferHeight / 2 + 120)
                {
                    Game1.instance.Exit();
                }
            }

            if (fading)
            {
                fadeout.alpha += 0.01f;

                if (fadeout.alpha > 1)
                {
                    SceneManager.setScene(new Level1());
                }
            }

            base.update(gameTime);
        }
    }
}
